package com.llkj.common.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.gyf.barlibrary.ImmersionBar;
import com.llkj.common.R;
import com.llkj.common.SupportFragment;
import com.llkj.common.base.app.BaseApp;
import com.llkj.common.base.delegate.IFragment;
import com.llkj.common.exception.ApiException;
import com.llkj.common.mvp.BasePresenter;
import com.llkj.common.mvp.IView;
import com.llkj.common.util.LoadDialogUtil;
import com.llkj.common.widget.titlebar.TitleBar;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import timber.log.Timber;

/**
 * author: zhu.px
 * e-mail: 1271901673@qq.com
 * time  : 2018/5/8
 * desc  :
 */
public abstract class BaseFragment<P extends BasePresenter> extends SupportFragment implements TitleBar.Delegate
        , IFragment, IView {

    protected Activity mContext;
    protected View mContentView;
    @Inject
    @Nullable
    protected P mPresenter;
    protected ImmersionBar mImmersionBar;
    private Unbinder unbinder;

    @Override
    public void showLoadingDialog(String title) {
        LoadDialogUtil.showLoading(mContext);
    }

    @Override
    public void hideLoadingDialog() {
        LoadDialogUtil.hideLoading();
    }

    @Override
    public void showMessage(String message) {
        ToastUtils.showShort(message);
    }

    @Override
    public void showMessage(int resId) {
        ToastUtils.showShort(resId);
    }

    @Override
    public void showErrorException(ApiException e) {
        ToastUtils.showShort(e.getMessage());
    }

    @Override
    public void finishRefresh() {
    }

    @Override
    public void showRefreshLoading() {

    }

    @Override
    public void onClickLeftCtv() {
    }

    @Override
    public void onClickTitleCtv() {

    }

    @Override
    public void onClickRightCtv() {

    }

    @Override
    public void onClickRightSecondaryCtv() {

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mContext = (Activity) context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mContentView == null) {
            init(savedInstanceState);
        } else {
            ViewGroup parent = (ViewGroup) mContentView.getParent();
            if (parent != null) {
                parent.removeView(mContentView);
            }
        }
        return mContentView;
    }

    private void init(Bundle savedInstanceState) {
        mContentView = LayoutInflater.from(mContext).inflate(getLayoutResId(), null);
        unbinder = ButterKnife.bind(this, mContentView);
        Bundle extras = getArguments();
        if (null != extras) {
            getBundleExtras(extras);
        }
        if (isSetStatusBarAlpha()) {
//            if (titleBar != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//                titleBar.getLayoutParams().height = ConvertUtils.dp2px(45) + BarUtils.getStatusBarHeight();
//                titleBar.setPadding(0, BarUtils.getStatusBarHeight(), 0, 0);
//            }
        }
        initView();
        initData();
        initListener();
    }

    public boolean isSetStatusBarAlpha() {
        return false;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        View titleBar = view.findViewById(getStatusTitleBar());
        if (titleBar != null) {
            if (titleBar instanceof TitleBar) {
                ((TitleBar) titleBar).setDelegate(this);
            }
            ImmersionBar.setTitleBar(mContext, titleBar);
        }
    }

    /**
     * 需要沉浸的view id
     *
     * @return
     */
    @IdRes
    protected int getStatusTitleBar() {
        return R.id.title_bar;
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        lazyLoad();
    }

    /**
     * 同级下的懒加载＋ViewPager下的懒加载的结合回调方法
     */
    protected void lazyLoad() {
    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        if (isSetStatusBarAlpha()) {
            mImmersionBar = ImmersionBar.with(this);
            mImmersionBar.init();
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setupFragmentComponent(BaseApp.getInstance().getAppComponent());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (unbinder != null) {
            unbinder.unbind();
        }
        if (mPresenter != null) {
            mPresenter.onDestroy();
        }
        if (useEventBus()) {
            EventBus.getDefault().unregister(this);
        }
        if (mImmersionBar != null) {
            mImmersionBar.destroy();
        }
    }

    /**
     * 通过Class跳转界面
     */
    public void startActivity(Class<?> clz) {
        startActivity(clz, null);
    }

    /**
     * 含有Bundle通过Class跳转界面
     */
    public void startActivity(Class<?> clz, Bundle bundle) {
        KeyboardUtils.hideSoftInput(mContext);
        Intent intent = new Intent();
        intent.setClass(mContext, clz);
        if (bundle != null)
            intent.putExtras(bundle);
        startActivity(intent);
    }

    /**
     * 通过Class跳转界面
     **/
    public void startActivityForResult(Class<?> cls, int requestCode) {
        startActivityForResult(cls, null, requestCode);
    }

    /**
     * 含有Bundle通过Class跳转界面
     **/
    public void startActivityForResult(Class<?> cls, Bundle bundle,
                                       int requestCode) {
        KeyboardUtils.hideSoftInput(mContext);
        Intent intent = new Intent();
        intent.setClass(mContext, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }
}
